﻿using CardMonitor.Model;
using CardMonitor.Utils;
using CardMonitor.Windows;
using DotNetSpeech;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace CardMonitor.Pages
{
    /// <summary>
    /// PrintOptionPage.xaml 的交互逻辑
    /// </summary>
    public partial class PrintOptionPage : Page
    {
        CardInfo objIDCardInfo = null;
        MainWindow mainWindow = null;
        SpVoice speech = new SpVoice();
        public PrintOptionPage(MainWindow mainWin)
        {
            InitializeComponent();
            mainWindow = mainWin;
        }

        private void doPrint(object sender, RoutedEventArgs e)
        {
            PrintItemPage printItemPage = new PrintItemPage(mainWindow,new BillModel());
            NavigationService.GetNavigationService(this).Navigate(printItemPage);
            //string cardDevice = (string)App.getConfig().IDCardDevice;
            //if (!"1".Equals(cardDevice))
            //{
            //    goBillPrint((string)App.getConfig().IDCardCode);
            //    return;
            //}

            //string sMsg = string.Empty;
            //String sPath = System.Environment.CurrentDirectory + "\\";

            //try
            //{
            //    //读身份证对象
            //    objIDCardInfo = IDCardReader.ReadCardInfo(0, out sMsg, sPath);
            //    if (!string.IsNullOrEmpty(sMsg))
            //    {
            //        showMessage(sMsg);
            //    }
            //}
            //catch (Exception ex)
            //{
            //    showMessage("身份证读取失败");
            //}

            //if (objIDCardInfo != null)
            //{
            //    goBillPrint(objIDCardInfo.CardNo);
            //}
        }
        private void goBillPrint(String cardCode)
        {
            HttpPostWindow httpPostWindow = new HttpPostWindow();
            httpPostWindow.Owner = mainWindow;

            JObject paramJSON = new JObject();
            paramJSON.Add("cardCode", cardCode);
            paramJSON.Add("operation", "clientPrint");
            httpPostWindow.setParamObject(paramJSON);

            httpPostWindow.setOperation(HttpPostWindow.OperationType.getBillsByCardForPrint.ToString());
            bool result = (bool)httpPostWindow.ShowDialog();
            if (!result)
            {
                string message = httpPostWindow.getMessage();
                if (null == message || "".Equals(message))
                {
                    message = "请到人工窗口打印";
                }
                speech.Speak(message, SpeechVoiceSpeakFlags.SVSFlagsAsync);

                showMessage(message);

                return;
            }
            JArray resultArray = httpPostWindow.getResultArray();
            List<BillModel> bills = JsonConvert.DeserializeObject<List<BillModel>>(resultArray.ToString());
            if (null == bills || bills.Count == 0)
            {
                showMessage("未查询到单据");
                return;
            }
            PrintPage printPage = new PrintPage(bills, mainWindow);
            NavigationService.GetNavigationService(this).Navigate(printPage);
        }
        private void showMessage(String message)
        {
            InfoWindow infoWindow = new InfoWindow(message);
            infoWindow.Owner = mainWindow;
            infoWindow.ShowDialog();
        }
        private void CloseWin(object sender, RoutedEventArgs e)
        {
           NavigationService.GetNavigationService(this).GoBack();
            //Window win = (Window)this.Parent;
            //win.Close();
        }
    }
}
